<template> 
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span> 
    </el-card>
    <div class="table-container">
      <el-table ref="roleTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        
         
        <el-table-column label="流水单号"  width="360"    align="center">
          <template slot-scope="scope">{{scope.row.outTradeNo}}</template>
        </el-table-column> 
        <el-table-column label="交易金额" align="center">
          <template slot-scope="scope">{{scope.row.amount}}元</template>
        </el-table-column>  
        <el-table-column label="类型"   align="center">
          <template slot-scope="scope">{{scope.row.type == 0 ? '商品下单': scope.row.type == 1 ? '拼团': scope.row.type == 2 ? '购买会员':''}}</template>
        </el-table-column>  
        <el-table-column label="状态"   align="center">
          <template slot-scope="scope">{{scope.row.state == 0 ? '待支付': scope.row.type == 1 ? '已支付': scope.row.type == 2 ? '退款':''}}</template>
        </el-table-column>  
        <el-table-column label="关联用户"     align="center">
          <template slot-scope="scope">{{scope.row.member.nickname}}</template>
        </el-table-column>  
        <el-table-column label="日期"    align="center">
          <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
        </el-table-column>   
        <!-- <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope"> 
            <el-row>
            <el-button size="mini"
                       type="text"
                       @click="handleUpdate(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini"
                       type="text"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
            </el-row>
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑分类':'添加分类'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="formData"  ref="roleForm" label-width="150px" size="small">
        <el-form-item label="图标：">
          <single-upload v-model="formData.icon"></single-upload>
        </el-form-item> 
        <el-form-item label="名称：">
          <el-input v-model="formData.title" style="width: 300px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import { fetchList } from '@/api/flow'; 
  import {formatDate} from '@/utils/date';

  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10, 
  };
  const defaultData = {
    title: '',
    icon: '', 
  };
  export default {
    name: 'categoryList', 
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        formData: Object.assign({}, defaultData),
        isEdit: false
      }
    },
    created() {
      this.getList();
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    methods: {  
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.formData = Object.assign({},defaultData);
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let id = [];
          id.push(row.id);
          let params=new URLSearchParams();
          params.append("id",id);
          del(params).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.isEdit = true; 
        for (const key in this.formData) {
            if (row[key] != null && row[key] != undefined) { 
              this.formData[key] = row[key]
            }
        } 
        this.formData.id = row.id; 
      },
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.isEdit) {
            edit(this.formData).then(response => {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            add(this.formData).then(response => {
              this.$message({
                message: '添加成功！',
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          }
        })
      },  
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      }
    }
  }
</script>
<style></style>


